﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.SystemService;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows.Media.Imaging;

namespace CommunitySystem.PageView.SystemManager
{
    public partial class RoleMenuWindow : ChildWindow
    {
        private SystemServiceClient client = null;
        private int _RoleID = 0;
        public RoleMenuWindow(int roleid)
        {
            InitializeComponent();
            _RoleID = roleid;
            client = new SystemServiceClient();
            client.GetRoleMenuAsync(_RoleID);
            client.GetRoleMenuCompleted += new EventHandler<GetRoleMenuCompletedEventArgs>(client_GetRoleMenuCompleted);
        }
        private ObservableCollection<RoleMenu> _rights = null;
        void client_GetRoleMenuCompleted(object sender, GetRoleMenuCompletedEventArgs e)
        {
            _rights = e.Result;
            LoadTree(_rights);
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            ObservableCollection<RoleMenu> menus = new ObservableCollection<RoleMenu>(_rights.Where(c => c.ID > 0 || c.RoleID > 0).ToArray());
            foreach (var m in menus)
            {
                m.Menu = null;
            }
            client.UpdateRoleMenuAsync(menus);
            client.UpdateRoleMenuCompleted += (sender1, e2) =>
            {
                this.DialogResult = true;
            };
            
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        void LoadTree(ObservableCollection<RoleMenu> rights)
        {
            
            foreach (var i in rights.Where(c => c.Menu.PID == 0))
            {
                TreeViewItem item = new TreeViewItem();
                CheckBox cb = new CheckBox();
                cb.DataContext = i;
                if (i.RoleID > 0)
                {
                    cb.IsChecked = true;
                }
                //item.SetBinding(TreeViewItem.HeaderProperty, new System.Windows.Data.Binding() { Path = new PropertyPath("Title")});
                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                if (i.Menu.icon != null && i.Menu.icon.Length > 0)
                {
                    Image img = new Image() { HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, Width = 16, Height = 16, Stretch = Stretch.Fill, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                    BitmapImage b = new BitmapImage();
                    Stream mm = new MemoryStream();
                    mm.Write(i.Menu.icon, 0, i.Menu.icon.Length);
                    b.SetSource(mm);
                    img.Source = b;
                    using (MemoryStream stream = new MemoryStream(i.Menu.icon))
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        BitmapImage b2 = new BitmapImage();
                        b2.SetSource(stream);
                        img.Source = b2;
                    }
                    sp.Children.Add(img);
                }
                sp.Children.Add(new TextBlock() { Text = i.Menu.Title, FontSize = 12, Foreground = new SolidColorBrush(Colors.Black), Margin = new Thickness(4, 0, 0, 0) });
                cb.Content = sp;
                cb.Click += new RoutedEventHandler(cb_Click);
                item.Header = cb;
                cb.Tag = item;
                sp.DataContext = i;
                item.DataContext = i;
                AddChildNode(item, i, rights);
                
                //item.MouseLeftButtonDown += new MouseButtonEventHandler(sp_MouseLeftButtonDown);
                RightsTree.Items.Add(item);
            }
             
        }

        void AddChildNode(TreeViewItem pitem, RoleMenu pmenu, ObservableCollection<RoleMenu> source)
        {
            foreach (var i in source.Where(c => c.Menu.PID == pmenu.Menu.ID))
            {
                TreeViewItem item = new TreeViewItem();
                CheckBox cb = new CheckBox();
                cb.DataContext = i;
                if (i.RoleID > 0)
                {
                    cb.IsChecked = true;
                }
                //item.SetBinding(TreeViewItem.HeaderProperty, new System.Windows.Data.Binding() { Path = new PropertyPath("Title")});
                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                if (i.Menu.icon != null && i.Menu.icon.Length > 0)
                {
                    Image img = new Image() { HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, Width = 16, Height = 16, Stretch = Stretch.Fill, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                    BitmapImage b = new BitmapImage();
                    Stream mm = new MemoryStream();
                    mm.Write(i.Menu.icon, 0, i.Menu.icon.Length);
                    b.SetSource(mm);
                    img.Source = b;
                    using (MemoryStream stream = new MemoryStream(i.Menu.icon))
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        BitmapImage b2 = new BitmapImage();
                        b2.SetSource(stream);
                        img.Source = b2;
                    }
                    sp.Children.Add(img);
                }
                sp.Children.Add(new TextBlock() { Text = i.Menu.Title, FontSize = 12, Foreground = new SolidColorBrush(Colors.Black), Margin = new Thickness(4, 0, 0, 0) });
                cb.Content = sp;
                cb.Click += new RoutedEventHandler(cb_Click);
                item.Header = cb;
                cb.Tag = item;
                sp.DataContext = i;
                item.DataContext = i;
                AddChildNode(item, i, source);


                pitem.Items.Add(item);
            }
        }

        void cb_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            RoleMenu rm = cb.DataContext as RoleMenu;
            if (cb.IsChecked == true)
            {
                rm.RoleID = _RoleID;
                CheckParentItem(cb.Tag as TreeViewItem);
                CheckChildItem(cb.Tag as TreeViewItem);
            }
            else
            {
                rm.RoleID = 0;
                UnCheckChildItem(cb.Tag as TreeViewItem);
            }
        }

        void CheckParentItem(TreeViewItem item)
        {
            TreeViewItem tvi = item.GetParentTreeViewItem();
            if (tvi != null)
            {
                CheckBox cb = tvi.Header as CheckBox;
                cb.IsChecked = true;
                RoleMenu rm = cb.DataContext as RoleMenu;
                rm.RoleID = _RoleID;
                CheckParentItem(tvi);
            }
        }

        void CheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = true;
                    RoleMenu rm = cb.DataContext as RoleMenu;
                    rm.RoleID = _RoleID;
                    CheckChildItem(tvi);
                }
            }
        }

        void UnCheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = false;
                    RoleMenu rm = cb.DataContext as RoleMenu;
                    rm.RoleID = 0;
                    UnCheckChildItem(tvi);
                }
            }
        }
        
    }
}

